package com.example.AIstudy.controller;

import com.alibaba.fastjson.JSON;
import com.example.AIstudy.domain.User;
import com.example.AIstudy.domain.UserToken;
import com.example.AIstudy.exception.AIstudyException;
import com.example.AIstudy.service.UserService;
import com.example.AIstudy.service.WechatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@RestController
@CrossOrigin
@RequestMapping("authenticate")
@Api(tags = "登录处理API")
public class AuthenticationController {
//    @Autowired
//    private JwtTokenUtil jwtTokenUtil;
//    @Autowired
//    PasswordEncoder passwordEncoder;
//    @Autowired
//    private UserDetailsService userDetailsService;

    @Autowired
    private UserService userService;
    @Autowired
    private WechatService wechatService;
    @GetMapping("/login")
    @ApiOperation("用户登录,根据传入的code返回用户的信息")
    public User login(@RequestParam String code) throws AIstudyException {
        try {
            //根据code获取到token和openid
            String openId=wechatService.codeToOpenId(code);

            //如果数据库中不存在用户的信息，需要插入一条数据
            if(userService.getUserByOpenID(openId)==null){
                User newUser = new User();
                newUser.setOpenid(openId);
                newUser.setNickname("微信用户");
                userService.insertUser(newUser);
                return newUser;
            }else {
                return userService.getUserByOpenID(openId);
            }
        }catch (Exception e){
            throw new AIstudyException(AIstudyException.ErrorType.QUERY_ERROR,"登录失败，请重试");
        }
    }
//    @GetMapping("/login")
//    @ApiOperation("用户登录")
//    public String login(@RequestParam String code) throws AIstudyException {
//
//        //根据code获取到token和openid
//        String tokenStr=wechatService.codeToOpenId(code);
//        UserToken userToken = JSON.parseObject(tokenStr, UserToken.class);
//
//        //根据token和openid获取到用户信息
//        String userInfo=wechatService.getUserInfo(userToken.getAccess_token(), userToken.getOpenid());
//        User user=JSON.parseObject(userInfo,User.class);
//
//        //如果数据库中不存在用户的信息，需要插入一条数据
//        if(userService.getUserByOpenID(user.getOpenid())==null){
//            userService.insertUser(user);
//        }
//        //根据openID获取userDetails，因为openID是唯一的
//        final UserDetails userDetails = userDetailsService.loadUserByUsername(user.getOpenid());
//        if(userDetails==null) {
//            throw new AIstudyException(AIstudyException.ErrorType.LOGIN_ERROR, "用户信息不存在");
//        }
//        //获取token
//        final String token = jwtTokenUtil.generateToken(userDetails);
//        return token;
//    }
//
//
////    @PostMapping("/register")
////    @ApiOperation("实现注册逻辑")
////    public String register(@RequestBody User user) throws AIstudyException {
////
////        userService.insertUser(user);
////        //注册后获取一个UserDetail的对象，构建token并返回token
////        final UserDetails userDetails = userDetailsService.loadUserByUsername(user.getUsername());
////        final String token = jwtTokenUtil.generateToken(userDetails);
////        return token;
////    }
//
}
